<?php
/**
 * Created by PhpStorm.
 * User: yinnannan
 * Date: 16/2/20
 * Time: 下午9:43
 */



class checkcode  {



    public function sendCodeEmail($parm){
        $result = DB::select('*')->from('sysuser')->where("email","=",$parm['email'])->execute()->as_array();


        $msg = null;
        $msg['flag'] = true;
        if(count($result)!=0){
            $msg['flag'] = false;
            $msg['data'] ='邮箱已绑定';
        }else{
            $result = DB::select('*')->from('syscheckcode')->where("email","=",$parm['email'])->order_by("createDate","desc")->limit(1)->offset(0)->execute()->as_array();
            $flag = true;
            date_default_timezone_set('PRC');
            if(count($result)==0){

                $flag = true;
                $msg['flag']=$flag;

            } else if(time()-strtotime($result[0]['createDate'])<60){

                $flag = false;
                $msg['flag']=false;
                $msg['data'] ='请不要重复发验证码';
            }
        }

        if($msg['flag']){
            DB::insert("syscheckcode",array('ValidateCode','`model`','`use`','`email`'))->values(array($parm['ValidateCode'],$parm['model'],$parm['use'],$parm['email']))->execute();
            $msg['flag']=true;
        }else{
            $msg['flag']=false;
        }
        return $msg;
    }
    public function sendBindPhone($parm){
        $result = DB::select('*')->from('sysuser')->where("phone","=",$parm['phone'])->execute()->as_array();


        $msg = null;
        $msg['flag'] = true;
        if(count($result)!=0){
            $msg['flag'] = false;
            $msg['data'] ='手机号已注册';
        }else{
            $result = DB::select('*')->from('syscheckcode')->where("phone","=",$parm['phone'])->order_by("createDate","desc")->limit(1)->offset(0)->execute()->as_array();
            $flag = true;
            date_default_timezone_set('PRC');
            if(count($result)==0){

                $flag = true;
                $msg['flag']=$flag;

            } else if(time()-strtotime($result[0]['createDate'])<60){

                $flag = false;
                $msg['flag']=false;
                $msg['data'] ='请不要重复发验证码';
            }
        }

        if($msg['flag']){
            DB::insert("syscheckcode",array('ValidateCode','`model`','`use`','`phone`'))->values(array($parm['ValidateCode'],$parm['model'],$parm['use'],$parm['phone']))->execute();
            $msg['flag']=true;
        }else{
            $msg['flag']=false;
        }
        return $msg;
    }
    public function sendCode($parm){
        $result = DB::select('*')->from('sysuser')->where("phone","=",$parm['phone'])->execute()->as_array();


        $msg = null;
        $msg['flag'] = true;
        if(count($result)==0&&$parm['action'] == "forget"){
            $msg['flag'] = false;
            $msg['data'] ='手机号不存在';
        }else if(count($result)!=0&&$parm['action'] == "send"){
            $msg['flag'] = false;
            $msg['data'] ='手机号已注册';
        }else{
            $result = DB::select('*')->from('syscheckcode')->where("phone","=",$parm['phone'])->order_by("createDate","desc")->limit(1)->offset(0)->execute()->as_array();
            $flag = true;
            date_default_timezone_set('PRC');
            if(count($result)==0){

                $flag = true;
                $msg['flag']=$flag;

            } else if(time()-strtotime($result[0]['createDate'])<60){

                $flag = false;
                $msg['flag']=false;
                $msg['data'] ='请不要重复发验证码';
            }
        }

        if($msg['flag']){
            DB::insert("syscheckcode",array('ValidateCode','`model`','`use`','`phone`'))->values(array($parm['ValidateCode'],$parm['model'],$parm['use'],$parm['phone']))->execute();
            $msg['flag']=true;
        }else{
            $msg['flag']=false;
        }
        return $msg;
    }


    public function check($parm){

        $result = DB::select('*')->from('syscheckcode')->where("phone","=",$parm['phone'])->order_by("createDate","desc")->limit(1)->offset(0)->execute()->as_array();
        return $result;
    }
}